package lichtruong.bk.cse;

import com.google.android.gms.maps.GoogleMap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class menu extends Activity implements LocationListener, OnClickListener {
	// GPS
	double[] LatLng;
	GoogleMap googleMap;
	boolean levelUser;
	static double latitude, longitude;
	LocationManager locationManager;
	Button register, login, auto, report, reported;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		checkLogin();
		register = (Button)findViewById(R.id.register);
		register.getBackground().setColorFilter(new LightingColorFilter(0x00FF00, 0x00FF00));
		login = (Button)findViewById(R.id.login);
		login.getBackground().setColorFilter(new LightingColorFilter(0x0000FF, 0x0000FF));
		auto = (Button)findViewById(R.id.auto);
		auto.getBackground().setColorFilter(new LightingColorFilter(0x000000, 0x000000));
		report = (Button)findViewById(R.id.report);
		report.getBackground().setColorFilter(new LightingColorFilter(0x000080, 0x000080));
		reported = (Button)findViewById(R.id.reported);
		reported.getBackground().setColorFilter(new LightingColorFilter(0xFF0000, 0xFF0000));
		if(levelUser){
			login.setText("Đăng Xuất");
		}
		// GPS
		latitude = 10.776126655419617;
		longitude = 106.64446815587598;
		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				this);
		// end GPS
		
		register.setOnClickListener(this); login.setOnClickListener(this);
		auto.setOnClickListener(this); report.setOnClickListener(this);
		reported.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.register:
			Intent register = new Intent(this, Register.class);
			startActivity(register);
			break;
		case R.id.login:
			if(levelUser){
				Database updateMail = new Database(menu.this);
				updateMail.open(); updateMail.updateLevelUser("0"); updateMail.close();
				Toast.makeText(getBaseContext(), "Đăng Xuất Thành Công", Toast.LENGTH_SHORT).show();
				login.setText("Đăng Nhập"); levelUser = false;
			}else{
				Intent login = new Intent(this, Login.class);
				login.putExtra("back", "1");
				startActivity(login);
			}
			break;
		case R.id.auto:
			Intent auto = new Intent(this, AuToGet.class);
			startActivity(auto);
			break;
		case R.id.report:
			if(levelUser){
				Intent report = new Intent(this, Report.class);
				startActivity(report);
			}else{
				AlertDialog.Builder builder = new AlertDialog.Builder(menu.this);
				builder.setMessage("Bạn Phải Đăng Nhập Trước Khi Thực Hiện Chức Năng Báo Cáo")
						.setTitle("  Tin Nhắn Từ Blue Road  ")
						.setPositiveButton("Đồng Ý",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Intent login = new Intent(menu.this, Login.class);
										login.putExtra("back", "2");
										startActivity(login);
									}
								})
						.setNegativeButton("Hủy",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.cancel();
									}
								}).setCancelable(false);
				builder.show();
			}
			break;
		case R.id.reported:
			Intent reported = new Intent(this, GetDataReported.class);
			startActivity(reported);
			break;
		}
	}
	
	// function return latitude and longitude
	public <Array> double[] locationCurrent() {
		new Report();
		double[] LatLng = { latitude, longitude };
		return LatLng;
	}

	// GPS

	@Override
	public void onLocationChanged(Location location) {
		latitude = location.getLatitude();
		longitude = location.getLongitude();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}
	
	// check login
	public void checkLogin(){
		Database getlevel = new Database(menu.this);
		getlevel.open();
		if(Integer.parseInt(getlevel.getData_USER().level) == 0){
			levelUser = false;
		}else{
			levelUser = true;
		}
		getlevel.close();
	}
}
